import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login.vue'
import Welcome from "@/views/welcome.vue";
import Index from "@/views/index.vue";
import User from "@/views/users.vue"
import store from '@/store'
import Roles from "@/views/roles.vue";
import Goods from "@/views/goods.vue";
import Params from "@/views/params.vue";
import Categories from "@/views/categories.vue";
import Orders from "@/views/orders.vue";
import Reports from "@/views/reports.vue";
import Rights from "@/views/rights.vue";
import Add from "@/views/add.vue";


Vue.use(VueRouter)

const routes = [
    {

        path: '/',
        redirect: '/login',

    },
    {
        path: '/login',
        component: Login,
    },
    {
        path: '/welcome',
        component: Welcome,
        children: [{
            path: '/',
            component: Index
        },
        {
            path:'/users',
            component: User
        },
            {
            path:'/roles',
                component: Roles,
            },

            {
                path:'/goods',
                component: Goods,
            }, {
                path:'/params',
                component: Params,
            } ,{
                path:'/categories',
                component: Categories,
            }, {
                path:'/orders',
                component: Orders,
            },
            {
                path:'/reports',
                component: Reports,
            },
            {
                path:'/rights',
                component:Rights
            },
{
    path:'/add',
        component: Add,
},
        ]

    }

]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    const token = store.state.user.token;
    if (to.path !== "/login" && !token) return next("/login");
    next();
});


export default router
